using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;

namespace Elementum
{
    public class MechanismGound : GroundObject
    {

        public List<ModifierContain> PositiveAction = new List<ModifierContain>();
        public List<ModifierContain> NegetiveAction = new List<ModifierContain>();

        public int PositiveActionCount;
        public int NegetiveActionCount;
        public int tempCount;


        protected bool _isActive;
        bool _iniActive;


        public MechanismGound(World world, Stage stage)
            : base(world, stage)
        {
            _body.SetType(BodyType.Kinematic);
            IsActive = false;
        }

        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                _isActive = value;
                _iniActive = value;
            }
        }

        public override void Restart()
        {
            base.Restart();
            //IsActive = _iniActive;
        }

        public override void ChangeState()
        {
            base.ChangeState();

            IsActive = !IsActive;
            if (ModifierContainer != null && !ModifierContainer.IsAllModifierDone())
            {
                return;
            }


            if (IsActive && PositiveAction.Count > 0)
            {
                this.ModifierContainer = PositiveAction[PositiveActionCount];
                PositiveActionCount++;
                PositiveActionCount %= PositiveAction.Count;
                foreach (ModifierContain c in NegetiveAction)
                    c.Conuter = 0;
            }
            else if (!IsActive && NegetiveAction.Count > 0)
            {
                this.ModifierContainer = NegetiveAction[NegetiveActionCount];
                NegetiveActionCount++;
                NegetiveActionCount %= NegetiveAction.Count;
                foreach (ModifierContain c in PositiveAction)
                    c.Conuter = 0;
            }



        }


        Stack<bool> _activeStack = new Stack<bool>();

        public override void Update(int delta)
        {
            base.Update(delta);

            if (ModifierContainer != null && ModifierContainer.IsAllModifierDone() && _activeStack.Count() > 0)
            {
                bool active = _activeStack.Pop();
                ChangeState(active);
            }

        }

        public override void ChangeState(bool active)
        {
            base.ChangeState();

            if (ModifierContainer != null && !ModifierContainer.IsAllModifierDone())
            {
                _activeStack.Push(active);
                return;
            }
            IsActive = active;

            if (IsActive && PositiveAction.Count > 0)
            {
                this.ModifierContainer = PositiveAction[PositiveActionCount];
                PositiveActionCount++;
                PositiveActionCount %= PositiveAction.Count;
                foreach (ModifierContain c in NegetiveAction)
                    c.Conuter = 0;
            }
            else if (!IsActive && NegetiveAction.Count > 0)
            {
                this.ModifierContainer = NegetiveAction[NegetiveActionCount];
                NegetiveActionCount++;
                NegetiveActionCount %= NegetiveAction.Count;
                foreach (ModifierContain c in PositiveAction)
                    c.Conuter = 0;
            }

        }

    }
}
